package com.uvt.agenda.view;

import android.app.Dialog;
import android.content.Context;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.sleepbot.datetimepicker.time.RadialPickerLayout;
import com.sleepbot.datetimepicker.time.TimePickerDialog;
import com.uvt.agenda.AndroidUtils;
import com.uvt.agenda.Event;
import com.uvt.agenda.R;
import com.uvt.agenda.commands.*;
import org.joda.time.DateTime;

/**
 * Created by oana on 01/02/14.
 * 08:43
 */

public class NewEventView extends Dialog implements EventViewDecoratorInterface {

    /**
     * View elements
     */
    private Button cancelButton;
    private Button saveButton;
    private Button eventButton;
    private Button taskButton;
    private EditText eventName;
    private EditText eventDescription;
    private TextView startDayPicker;
    private TextView startTimePicker;
    private TextView endDayPicker;
    private TextView endTimePicker;
    private DatePickerDialog datePicker;
    private TimePickerDialog timePicker;
    private Event newEvent;

    private ImageButton editEvent;
    private ImageButton deleteButton;
    private Button closeButton;
    private TextView dialogHeader;
    private TextView eventHeader;


    /**
     * Working dates
     */
    private DateTime startDate = DateTime.now().withMinuteOfHour(0);
    private DateTime endDate = DateTime.now().plusHours(1).withMinuteOfHour(0);

    public NewEventView(Context context) {
        super(context, R.layout.new_event_dialog);

        this.newEvent = new Event();
        this.newEvent.setStartDateTime(this.startDate);
        initDialog();
    }

    public NewEventView(Context context, Event event) {
        super(context, R.layout.new_event_dialog);
        this.newEvent = event;
        this.startDate = event.getStartDateTime();
        this.endDate = event.getEndDateTime();
        initDialog();
        loadNodeInfo();
    }

    public NewEventView(Context context, DateTime start) {
        super(context, R.layout.new_event_dialog);
        Log.d("DATE", start.toString(AndroidUtils.DATE_FORMAT));
        this.startDate = start.withMinuteOfHour(0);
        this.endDate = start.plusHours(1).withMinuteOfHour(0);

        this.newEvent = new Event();
        this.newEvent.setStartDateTime(this.startDate);

        initDialog();
    }

    private void initDialog() {

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.new_event_dialog);
        this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        eventButton = (Button) findViewById(R.id.event_button);
        taskButton = (Button) findViewById(R.id.task_button);
        eventName = (EditText) findViewById(R.id.event_name);
        eventDescription = (EditText) findViewById(R.id.event_description);
        startDayPicker  = (TextView) findViewById(R.id.start_date_picker);
        startTimePicker = (TextView) findViewById(R.id.start_time_picker);
        endDayPicker  = (TextView) findViewById(R.id.end_date_picker);
        endTimePicker = (TextView) findViewById(R.id.end_time_picker);
        dialogHeader = (TextView) findViewById(R.id.dialog_header);
        eventHeader = (TextView) findViewById(R.id.event_header_title);

        saveButton = (Button) findViewById(R.id.save_btn);
        cancelButton = (Button) findViewById(R.id.cancel_btn);
        editEvent = (ImageButton) findViewById(R.id.edit_event);
        deleteButton = (ImageButton) findViewById(R.id.delete_event);
        closeButton  = (Button) findViewById(R.id.close_btn);

        datePicker = new DatePickerDialog(getContext(), startDate);
        timePicker = new TimePickerDialog(getContext());

        startDayPicker.setText(startDate.toString(AndroidUtils.DATE_FORMAT));
        startTimePicker.setText(startDate.toString(AndroidUtils.TIME_FORMAT));
        endDayPicker.setText(endDate.toString(AndroidUtils.DATE_FORMAT));
        endTimePicker.setText(endDate.toString(AndroidUtils.TIME_FORMAT));
        setClickListeners();
    }

    private void loadNodeInfo(){
        this.eventName.setText(newEvent.getName());
        this.eventDescription.setText(newEvent.getDescription());
        this.eventHeader.setText(newEvent.getName());
        this.dialogHeader.setText("Update event");
    }

    private void setClickListeners() {

        eventButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        taskButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newEvent.setName(eventName.getText().toString());
                newEvent.setDescription(eventDescription.getText().toString());
                Command command;
                if (newEvent.getName().length() != 0) {
                    if (newEvent.getID() != 0) {
                        command = new UpdateEventCommand(newEvent);
                    } else {
                        command = new AddEventCommand(newEvent);

                    }
                    CommandsManager.executeCommand(command);
                }
                dismiss();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }

        });

        // From pickers
        startDayPicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker.show();
                datePicker.setResultListener(new DialogResultListener() {
                    @Override
                    public void onDoneClick(Object result) {
                        DateTime selectedDate = (DateTime)result;
                        startDayPicker.setText(selectedDate.toString(AndroidUtils.DATE_FORMAT));
                        newEvent.setStartDateTime(selectedDate);
                        startDate = selectedDate;
                        if (endDate.isBefore(startDate))
                            endDate = selectedDate.plusHours(1);
                    }

                    @Override
                    public void onCancelClick() {

                    }
                });
            }
        });
        startTimePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timePicker.show();
                timePicker.setOnTimeSetListener(new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
                        newEvent.setStartDateTime(newEvent.getStartDateTime().withTime(hourOfDay, minute, 0, 0));
                        startTimePicker.setText(newEvent.getStartDateTime().toString(AndroidUtils.TIME_FORMAT));
                    }
                });
            }
        });

        // To pickers
        endDayPicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker.show();
                datePicker.setResultListener(new DialogResultListener() {
                    @Override
                    public void onDoneClick(Object result) {
                        DateTime selectedDate = (DateTime)result;
                        if (selectedDate.isAfter(startDate)) {
                            endDayPicker.setText(selectedDate.toString(AndroidUtils.DATE_FORMAT));
                            newEvent.setEndDateTime(selectedDate);
                        } else {
                            Toast.makeText(getContext(), "End date must be after start date", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancelClick() {

                    }
                });
            }
        });
        endTimePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timePicker.show();
                timePicker.setOnTimeSetListener(new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
                        newEvent.setEndDateTime(newEvent.getEndDateTime().withTime(hourOfDay, minute, 0, 0));
                        endTimePicker.setText(newEvent.getEndDateTime().toString(AndroidUtils.TIME_FORMAT));
                    }
                });
            }
        });

        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        editEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleViewMode();
            }
        });

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (newEvent.getID() != 0) {
                    CommandsManager.executeCommand(new DeleteEventCommand(newEvent));

                    Toast.makeText(getContext(), "Event deleted", Toast.LENGTH_SHORT).show();
                }
                dismiss();
            }
        });
    }

    public EventViewDecoratorInterface topLevel = null;

    @Override
    public void activateUI() {
        eventName.setInputType(InputType.TYPE_CLASS_TEXT);
        eventDescription.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

        setClickListeners();
        eventName.setVisibility(View.VISIBLE);
        dialogHeader.setVisibility(View.VISIBLE);
        saveButton.setVisibility(View.VISIBLE);
        cancelButton.setVisibility(View.VISIBLE);

        closeButton.setVisibility(View.GONE);
        editEvent.setVisibility(View.GONE);
        deleteButton.setVisibility(View.GONE);
        eventHeader.setVisibility(View.GONE);
        eventDescription.setEnabled(true);
        eventName.setEnabled(true);
    }

    public void disableEditing(){
        Log.e("DISABLE", "disable editing");
        eventButton.setOnClickListener(null);
        taskButton.setOnClickListener(null);
        saveButton.setOnClickListener(null);
        cancelButton.setOnClickListener(null);
        // From pickers
        startDayPicker.setOnClickListener(null);
        startTimePicker.setOnClickListener(null);

        // To pickers
        endDayPicker.setOnClickListener(null);
        endTimePicker.setOnClickListener(null);

        eventName.setInputType(InputType.TYPE_NULL);
        eventDescription.setInputType(InputType.TYPE_NULL);

        eventName.setVisibility(View.GONE);
        dialogHeader.setVisibility(View.GONE);
        saveButton.setVisibility(View.GONE);
        cancelButton.setVisibility(View.GONE);

        closeButton.setVisibility(View.VISIBLE);
        editEvent.setVisibility(View.VISIBLE);
        deleteButton.setVisibility(View.VISIBLE);
        eventHeader.setVisibility(View.VISIBLE);


    }

    public boolean toggleViewMode(){
        if (topLevel==null) {
            this.topLevel = new EventViewDecorator(this);
            this.topLevel.activateUI();
            return true;
        } else {
            this.activateUI();
            topLevel = null;
            return true;
        }

    }


}
